package com.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

//import org.apache.commons.lang.StringUtils;

/**
 * 日期Util类
 */
public class DateUtil {
	public static String DEFAULT_DATE_PATTERN = "yyyyMMdd";
	public static String LONG_DATETIME_TEXT_PATTERN="yyyy-MM-dd'T'HH:mm:ss";
	public static String DATETIME_PATTERN="yyyyMMddHHmmss";
	public static String LONG_DATE_TEXT_PATTERN="yyyy-MM-dd";

	/**
	 * 使用预设Format格式化Date成字符串
	 */
	public static String formatDateByDefaultPattern(Date date) {
		return date == null ? "" : format(date, DEFAULT_DATE_PATTERN);
	}

	/**
	 * 使用参数Format格式化Date成字符串
	 */
	public static String format(Date date, String pattern) {
		return date == null ? "" : new SimpleDateFormat(pattern).format(date);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 * strDate如：yyyy-MM-dd, 2016-05-21 转成一个Date
	 */
	public static Date parseByDefaultDatePattern(String strDate) throws ParseException {
		return strDate=="" ? null : parse(strDate,LONG_DATE_TEXT_PATTERN);
	}

	/**
	 * 使用预设格式将字符串转为Date
	 * strDate如：yyyyMMdd, 20160521 转成一个Date
	 */
	public static Date parseByDefaultDatePattern2(String strDate) throws ParseException {
		return strDate=="" ? null : parse(strDate,DEFAULT_DATE_PATTERN);
	}

	/**
	 * 使用参数Format将字符串转为Date
	 */
	public static Date parse(String strDate, String pattern) throws ParseException {
		return strDate=="" ? null : new SimpleDateFormat(pattern).parse(strDate);
	}

//	/**
//	 * 使用预设格式将字符串转为Date
//	 */
//	public static Date parseByDefaultDatePattern(String strDate) throws ParseException {
//		return StringUtils.isBlank(strDate) ? null : parse(strDate,DEFAULT_DATE_PATTERN);
//	}
//
//	/**
//	 * 使用参数Format将字符串转为Date
//	 */
//	public static Date parse(String strDate, String pattern) throws ParseException {
//		return StringUtils.isBlank(strDate) ? null : new SimpleDateFormat(pattern).parse(strDate);
//	}

	/**
	 * 在日期上增加数个整月
	 */
	public static Date addMonth(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, n);
		return cal.getTime();
	}


	/**
	 * 获得月末日
	 */
	public static Date getLastDayOfMonth(String year, String month, int flag) {

		Calendar cal = Calendar.getInstance();
		//年
		cal.set(Calendar.YEAR, Integer.parseInt(year));
		//月，因为Calendar里的月是从0开始，所以要-1
		cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
		//日，设为一号
		cal.set(Calendar.DATE, 1);
		//月份加一，得到下个月的一号
		cal.add(Calendar.MONTH, flag);
		//下一个月减一为本月最后一天
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 获得月末日: flag=1:获得上月月末日 flag=0：获得本月月末日
	 */
	public static Date getLastDayOfMonth(Date date, int flag) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		//日，设为一号
		cal.set(Calendar.DATE, 1);
		//月份加一，得到下个月的一号
		cal.add(Calendar.MONTH, flag);
		//下一个月减一为本月最后一天
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 获得周末日
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.add(Calendar.DATE, -1);
		return cal.getTime();
	}

	/**
	 * 在日期上增加数天
	 * @param date
	 * @param n
	 * @return
	 */
	public static Date addDay(Date date, int n) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, n);
		return cal.getTime();
	}

	/**
	 * 得到当前时间
	 * @return HHmmss
	 */
	public static String GetCurrentTime() {
		StringBuffer result = new StringBuffer();
		Calendar calendar = Calendar.getInstance();
		int h = calendar.get(Calendar.HOUR_OF_DAY);
		int m = calendar.get(Calendar.MINUTE);
		int s = calendar.get(Calendar.SECOND);
		if (h < 10) {
			result.append("0");
		}
		result.append(h);
		if (m < 10) {
			result.append("0");
		}
		result.append(m);
		if (s < 10) {
			result.append("0");
		}
		result.append(s);
		return result.toString();
	}

	/**
	 * 得到当前日期时间
	 * @return yyyyMMdd
	 */
	public static String getCurrDate() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMdd");
		String s = outFormat.format(now);
		return s;
	}
	/**
	 * 获得当前日期
	 * @return yyyy-MM-dd
	 */
	public static String get10CurrDate(){
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd");
		String s = outFormat.format(now);
		return s;
	}
	/**
	 * 根据输入日获得本周周一yyyy-MM-dd
	 * @param dateStr
	 * @return
	 */
	public static String getMondayOfWeek(String dateStr){
		try {
			Date date = parse(dateStr,LONG_DATE_TEXT_PATTERN);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			return format(cal.getTime(), LONG_DATE_TEXT_PATTERN);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	/**
	 * 根据输入日获得本周周日yyyy-MM-dd
	 * @param dateStr
	 * @return
	 */
	public static String getSundayOfWeek(String dateStr){
		try {
			Date date = parse(dateStr,LONG_DATE_TEXT_PATTERN);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			cal.add(Calendar.DATE, 6);
			return format(cal.getTime(), LONG_DATE_TEXT_PATTERN);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	/**
	 * 根据周中任意日期计算出该周内7天日期数据
	 * @param dateStr
	 * @return
	 */
	public static String[] getWeekDayArray(String dateStr){
		String[] dateArray = new String[7];
		try {
			Date date = parse(dateStr,LONG_DATE_TEXT_PATTERN);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			int i = 0;
			while(i<7){
				dateArray[i]=format(cal.getTime(), LONG_DATE_TEXT_PATTERN);
				cal.add(Calendar.DATE, 1);
				i++;
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		return dateArray;
	}
	/**
	 * 日期加减
	 * @param dateStr
	 * @param days
	 * @return
	 */
	public static String dateAddDays(String dateStr,int days){
		try {
			Date date = parse(dateStr,LONG_DATE_TEXT_PATTERN);
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.DATE, days);
			return format(cal.getTime(), LONG_DATE_TEXT_PATTERN);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return "";
		}
	}
	/**
	 * 得到当前年份
	 * @return yyyy
	 */
	public static String getCurrYear() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy");
		String s = outFormat.format(now);
		return s;
	}

	/**
	 * 得到当前日期时间
	 * @return yyyyMMddHHmmss
	 */
	public static String getCurrDateTime() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String s = outFormat.format(now);
		return s;
	}


	/**
	 * 得到当前日期时间
	 * @return yyyy-MM-dd HH:mm:ss
	 */
	public static String getCurrDateTimeStand() {
		Date now = new Date();
		SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s = outFormat.format(now);
		return s;
	}

	public static int getYear(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.YEAR);
	}

	public static int getMonth(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.DAY_OF_MONTH);
	}

	public static int getHour(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.HOUR_OF_DAY);
	}

	public static int getMinute(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.MINUTE);
	}

	public static int getSecond(Date date) {
		Calendar cld = Calendar.getInstance();
		cld.setTime(date);
		return cld.get(Calendar.SECOND);
	}


	/**
	 * 获得两天之间相差天数 date-date1
	 * @param date
	 * @param date1
	 * @return
	 */
	public static int getDiffDate(Date date, Date date1) {
		return (int) ((date.getTime() - date1.getTime()) / (24 * 3600 * 1000));
	}

	/**
	 * 获取两个时间的秒差  date-date1
	 * @param date
	 * @param date1
	 * @return
	 */
	public static long getDiffTime(Date date, Date date1) {
		return (date.getTime() - date1.getTime()) / 1000;
	}

	/**
	 * 获取当前月第一天，返回日期格式为YYYYMMDD
	 * */
	public static String getFirstDayOfCurrMonth(){
		 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
	    c.add(Calendar.MONTH, 0);
	    c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
	    return format.format(c.getTime());
	}
	/**
	 * 获取当前月第一天，返回日期格式为YYYYMMDD
	 * */
	public static String getLastDayOfCurrMonth(){
		 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
	    return format.format(c.getTime());
	}

	public static void main(String[] args){
		try {
			System.out.println("------="+DateUtil.parseByDefaultDatePattern("2016-04-29"));
			System.out.println("------="+getCurrDateTimeStand());
			System.out.println("------="+getCurrYear());

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}